const router = require('koa-router')()
//采购管理模块
//待委派-采购中-入库中-已完成
router.prefix('/buy')
//1.创建采购订单
router.post('/', async (ctx, next) => {
    let id = ctx.request.body.userId;
    let money = ctx.request.body.money;
    let detail = ctx.request.body.detail;
    let posttime = ctx.request.body.posttime;
    let deadline = ctx.request.body.deadline;
    let sql = "insert into buy (buy_storeid, buy_detail, buy_posttime, buy_deadline, buy_money) values('" + id + "','" + detail + "', '" + posttime + "', '" + deadline + "', '" + money + "')";
    let result = await ctx.db.EXCUTE(sql, []);

    if (result.affectedRows) {
        ctx.body = {
            "status": 200,
            "msg": '采购订单创建成功'
        }
    }
})
//2.修改订单
router.put('/', async (ctx, next) => {
    let id = ctx.request.body.buyId;
    let money = ctx.request.body.money;
    let detail = ctx.request.body.detail;
    let posttime = ctx.request.body.posttime;
    let deadline = ctx.request.body.deadline;
    let sql2 = `update buy set buy_money = ${money}, buy_detail = '${detail}',buy_posttime = '${posttime}',buy_deadline = '${deadline}' WHERE buy_id = ${id}`;
    await ctx.db.EXCUTE(sql2, []);
    let obj = {
        msg: "编辑成功",
        status: 200
    }
    ctx.body = JSON.stringify(obj);
})

//3.删除订单
router.delete('/', async (ctx, next) => {
    let sql = `delete from buy where buy_id='${ctx.request.body.buyId}'`
    let results = await ctx.db.EXCUTE(sql, []);
    if (results.affectedRows > 0) {
        let obj = {
            status: 200,
            msg: '删除成功'
        }
        ctx.body = JSON.stringify(obj);
    }
})

//4.查找订单
router.get('/', async (ctx, next) => {
    let storerId = ctx.request.query.storerId;
    let buyerId = ctx.request.query.buyerId;
    let countSql = "";
    if (storerId && buyerId) {
        countSql = `select count(*) from buy where buy_storeid = ${storerId} and buy_buyerid = ${buyerId}`;
    } else if (storerId) {
        countSql = `select count(*) from buy where buy_storeid = ${storerId}`;
    } else if (buyerId) {
        countSql = `select count(*) from buy where buy_buyerid = ${buyerId}`;
    } else {
        countSql = `select count(*) from buy`;
    }
    let countResults = await ctx.db.EXCUTE(countSql, []);
    let total = countResults[0]["count(*)"];
    let pageSize = Number(ctx.request.query.size)
    let page = ctx.request.query.page
    let startRow = total > 0 ? ((page - 1) * pageSize) : 0;
    //实现分页限制查找
    let limitSql;
    if (storerId && buyerId) {
        limitSql = `select * from buy where buy_storeid = ${storerId} and buy_buyerid = ${buyerId} order by buy_id desc limit ${startRow},${pageSize}`;
    } else if (storerId) {
        limitSql = `select * from buy where buy_storeid = ${storerId} order by buy_id desc limit ${startRow},${pageSize}`;
    } else if (buyerId) {
        limitSql = `select * from buy where buy_buyerid = ${buyerId} order by buy_id desc limit ${startRow},${pageSize}`;
    } else {
        limitSql = `select * from buy order by buy_id desc limit ${startRow},${pageSize}`;
    }
    let results = await ctx.db.EXCUTE(limitSql, []);
    for (let i = 0; i < results.length; i++) {
        if (results[i].buy_buyerid) {
            let sql3 = `select user_name from users where user_id = ${results[i].buy_buyerid}`;
            let res3 = await ctx.db.EXCUTE(sql3, []);
            let buyerName = res3[0].user_name;
            results[i].buyerName = buyerName;
            let sql4 = `select user_name from users where user_id = ${results[i].buy_storeid}`;
            let res4 = await ctx.db.EXCUTE(sql4, []);
            let storerName = res4[0].user_name;
            results[i].storerName = storerName;
        }
    }
    let obj = {
        length: total,
        results: results,
        status: 200
    }
    ctx.body = JSON.stringify(obj);
})

//5.委派采购订单
router.post('/distribution', async (ctx, next) => {
    let id = ctx.request.body.buyId;
    let buyerId = ctx.request.body.buyerId;
    // let financetype = "支出";
    //委派订单，更改订单信息
    // let sql = `select user_id from users where user_name = '${buyerName}'`;
    // let result = await ctx.db.EXCUTE(sql, []);
    // let buyerId = result[0].user_id;
    let sql2 = `update buy set buy_buyerid = ${buyerId},buy_type = '采购中' WHERE buy_id = ${id}`;
    await ctx.db.EXCUTE(sql2, []);
    // //插入支出
    // let sql4 = `select buy_money from buy where buy_id = '${id}' `;
    // let result4 = await ctx.db.EXCUTE(sql4, []);
    // let money = result4[0].buy_money;
    // let sql3 = "insert into finance (finance_type, finance_money, finance_orderid) values('" + financetype + "','" + money + "', '" + id + "')";
    // await ctx.db.EXCUTE(sql3, []);
    let obj = {
        msg: "委派成功",
        status: 200
    }
    ctx.body = JSON.stringify(obj);
})

//6.原料入库请求
router.post('/inRequire', async (ctx, next) => {
    let id = ctx.request.body.buyId;
    let detail = ctx.request.body.detail;
    let kind = "原料入库";
    let sql = "insert into requires (require_kind, require_orid, require_detail) values('" + kind + "','" + id + "','" + detail + "')";
    let sql2 = `update buy set buy_type = '入库中' WHERE buy_id = ${id}`;
    await ctx.db.EXCUTE(sql, []);
    await ctx.db.EXCUTE(sql2, []);
    let obj = {
        msg: "入库请求成功",
        status: 200
    }
    ctx.body = JSON.stringify(obj);
})

//7.查询采购员的下拉列表
router.get('/buyers', async (ctx, next) => {
    let sql = `select * from users where user_roleid = (select role_id from roles where role_desc = '采购员')`;
    let results = await ctx.db.EXCUTE(sql, []);
    if (results) {
        let obj = {
            status: 200,
            results: results
        }
        ctx.body = JSON.stringify(obj);
    }
})

//8.获取仓库管理员下拉列表
router.get('/storers', async (ctx, next) => {
    let sql = `select * from users where user_roleid = (select role_id from roles where role_desc = '仓库管理员')`;
    let results = await ctx.db.EXCUTE(sql, []);
    if (results) {
        let obj = {
            status: 200,
            results: results
        }
        ctx.body = JSON.stringify(obj);
    }
})
//查询原材料的下拉列表
router.get('/goods', async (ctx, next) => {
    let sql = `select * from goods where goods_type = 'material'`;
    let results = await ctx.db.EXCUTE(sql, []);
    if (results) {
        let obj = {
            status: 200,
            results: results
        }
        ctx.body = JSON.stringify(obj);
    }
})
module.exports = router